import * as actionTypes from './actionTypes'
import axios from 'axios'
import * as api from '../../public/api'
import qs from 'qs'
import * as message from '../../components/Msg/msg'
import Immutable, { isImmutable } from 'immutable'
import { getUInfo, setUInfo } from '../../session/session'

export const setPath = (param) => ({ type: actionTypes.setPath, data: param })
export const setUrl = (param) => ({ type: actionTypes.setUrl, data: param })
export const bookInfoTableData = (param) => ({ type: actionTypes.bookInfoTableData, data: param })
export const borrowingRecordTableData = (param) => ({ type: actionTypes.borrowingRecordTableData, data: param })
export const myBookFriendsTableData = (param) => ({ type: actionTypes.myBookFriendsTableData, data: param })
export const bookInfo = (param) => ({ type: actionTypes.bookInfo, data: param })
export const bookRecommend = (param) => ({ type: actionTypes.bookRecommend, data: param })
export const askFriend = (param) => ({ type: actionTypes.askFriend, data: param })
export const setHeight = (param) => ({ type: actionTypes.setHeight, data: param })
export const requestInfo = (param) => ({ type: actionTypes.setRequestInfo, data: param })

// 图书信息
export const setBookAll = (param) => (dispatch) => {
  console.log(JSON.stringify(param))
  axios.post(api.getAll, param, {
    headers: {
      'Content-Type': 'application/json',
    }
  }).then((response) => {
    const { code, data } = response.data
    const { records } = data
    if (code === 0) {
      dispatch(bookInfoTableData(Immutable.fromJS(records)))
    } else {
      message.error('表格初始化失败！')
    }
  }).catch((err) => {
    message.error(err)
  })
}
// 借阅记录
export const setBorrowingRecord = (param) => (dispatch) => {
  axios.post(api.borrowingRecord, param, {
    headers: {
      'Content-Type': 'application/json',
    }
  }).then((response) => {
    const { code, data } = response.data
    const { records } = data
    if (code === 0) {
      dispatch(borrowingRecordTableData(Immutable.fromJS(records)))
    } else {
      message.error('表格初始化失败！')
    }
  }).catch((err) => {
    message.error(err)
  })
}
// 我的书友
export const setMyBookFriends = (param) => (dispatch) => {
  axios.post(api.myBookFriends, param, {
    headers: {
      'Content-Type': 'application/json',
    }
  }).then((response) => {
    const { code, data } = response.data
    if (code === 0) {
      dispatch(myBookFriendsTableData(Immutable.fromJS(data)))
    } else {
      message.error('表格初始化失败！')
    }
  }).catch((err) => {
    message.error(err)
  })
}
// 图书信息
export const setBookInfo = (id) => (dispatch) => {
  axios.post(api.getBookInfo + '/?id=' + id).then((response) => {
    const { book, tagName } = response.data.data
    if (response.data.code === 0) {
      dispatch(bookInfo(Immutable.fromJS({ book, tagName })))
    } else {
      message.error('查询失败！')
    }
  }).catch(err => {
    message.error(err)
  })
}
// 找书友
export const setBookRecommend = (bookId, uid) => (dispatch) => {
  axios.post(api.getBookRecommend, qs.stringify({ bookId, uid })).then((response) => {
    const { data } = response.data
    if (response.data.code === 0) {
      dispatch(bookRecommend(Immutable.fromJS(data)))
    } else {
      message.error('查询失败！')
    }
  }).catch(err => {
    message.error(err)
  })
}

// 问同学
export const setAskFriend = (bookId, uid) => (dispatch) => {
  axios.post(api.getAskFriend, qs.stringify({ bookId, uid })).then((response) => {
    const { data } = response.data
    if (response.data.code === 0) {
      dispatch(askFriend(Immutable.fromJS(data)))
    } else {
      message.error('查询失败！')
    }
  }).catch(err => {
    message.error(err)
  })
}
// 发送好友申请
export const setAddFriend = (uid, fid) => (dispatch) => {
  axios.get(api.addFriend + '?uid=' + uid + '&fid=' + fid).then((response) => {
    const { code, data } = response.data
    if (code === 0) {
      message.success(data)
    } else {
      message.error('发送好友申请失败！')
    }
  }).catch(err => {
    message.error('发送好友申请失败！')
  })
}
// 修改个人信息
export const editUserInfo = (param) => (dispatch) => {
  axios.post(api.editUserInfo, param, {
    headers: {
      'Content-Type': 'application/json',
    }
  }).then(response => {
    const { code, data, msg } = response.data
    if (code === 0) {
      setUInfo(data)
      message.success('修改成功!')
    } else {
      message.error(msg)
    }
  }).catch(err => {
    message.error(err)
  })
}
// 修改密码
export const changePassword = (param) => (dispatch) => {
  return new Promise((resolve, reject) => {
    const { username, password } = param
    axios.get(api.changePassword + '?username=' + username + '&newPassword=' + password).then(response => {
      const { code, msg } = response.data
      if (code === 0) {
        message.success('修改密码成功')
        resolve()
      } else {
        message.error(msg)
        reject()
      }
    }).catch(err => {
      message.error(err)
      reject()
    })
  })
}
// 获取好友申请消息
export const setRequestInfo = (uid) => (dispatch) => {
  axios.get(api.friendApply + '?uid=' + uid).then((response) => {
    const { code, data } = response.data
    if (code === 0) {
      dispatch(requestInfo(Immutable.fromJS(data)))
    } else {
      message.error('获取好友消息申请失败！')
    }
  }).catch(err => {
    message.error('获取好友消息申请失败！')
  })
}
// 同意好友申请
export const acceptRequestInfo = (param) => (dispatch) => {
  const { messageId, accept } = param
  const uinfo = JSON.parse(getUInfo())
  axios.get(api.accept + '?messageId=' + messageId + '&accept=' + accept).then((response) => {
    const { code, data } = response.data
    if (code === 0) {
      if (accept) {
        message.success('您已通过好友申请')
        dispatch(setRequestInfo(uinfo.readerId))
      } else {
        message.success('您已拒绝好友申请')
        dispatch(setRequestInfo(uinfo.readerId))
      }
    } else {
      message.error('好友申请处理失败！')
    }
  }).catch(err => {
    message.error('好友申请处理失败！')
  })
}